﻿namespace AppLab.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Data.Objects.DataClasses;
    using System.Linq;
    using AppLab.Data.Models;
    using AppLab.Web.Resources;
    using Wicresoft.AppFlow.Flows;
    using AppLab.Core;

    public class PublishSubmitData
    {
        [Key]
        [Display(AutoGenerateField = false)]
        public Guid FlowGuid { get; set; }
        [Display(AutoGenerateField = false)]
        public int MobileID { get; set; }
        [Display(AutoGenerateField = false)]
        public string MobileBrand { get; set; }
        [Display(AutoGenerateField = false)]
        public string MobileType { get; set; }
        [Display(AutoGenerateField = false)]
        public string MobileVersion { get; set; }
        [Display(AutoGenerateField = false)]
        public string AssignedTo { get; set; }
        [Display(AutoGenerateField = false)]
        public DateTime TestEndTime { get; set; }
        //[Display(AutoGenerateField = false)]
        //public int MediaClientID { get; set; }
        [Display(AutoGenerateField = false)]
        public string ReleaseState { get; set; }
        [Display(AutoGenerateField = false)]
        public int WebState { get; set; }
        [Display(AutoGenerateField = false)]
        public int WapState { get; set; }
        [Display(AutoGenerateField = false)]
        public int PreloadedState { get; set; }
        [Display(AutoGenerateField = false)]
        public int TysxSpaceState { get; set; }
        [Display(AutoGenerateField = false)]
        public int TysxBoLuState { get; set; }

        /// <summary>
        /// 保存手机的各种发布状态
        /// </summary>
        /// <param name="state"></param>
        /// <param name="dc"></param>
        private void ReleaseSate(int state, PMSEntities dc)
        {
            var e = new MobileRelease();

            e.mobileid = this.MobileID;
            e.releaseTypeID = state;
            dc.MobileReleases.AddObject(e);

            dc.SaveChanges();
        }

        /// <summary>
        /// 删除该手机已发布的状态
        /// </summary>
        /// <param name="dc"></param>
        private void DeleteRelease(PMSEntities dc)
        {
            List<MobileRelease> list = dc.MobileReleases.Where(p => p.mobileid == this.MobileID).ToList();
            foreach (MobileRelease e in list)
            {
                dc.MobileReleases.DeleteObject(e);
                dc.SaveChanges();
            }
        }

        public Guid ToDataBase()
        {
            try
            {
                using (var dc = PMSEntities.Create())
                {
                    // 删除该手机已发布的状态
                    DeleteRelease(dc);

                    //保存手机的各种发布状态
                    if (WebState != 0)
                        ReleaseSate(WebState, dc);
                    if (WapState != 0)
                        ReleaseSate(WapState, dc);
                    if (PreloadedState != 0)
                        ReleaseSate(PreloadedState, dc);
                    if (TysxSpaceState != 0)
                        ReleaseSate(TysxSpaceState, dc);
                    if (TysxBoLuState != 0)
                        ReleaseSate(TysxBoLuState, dc);

                    var flow = dc.tysx_AppFlow.First(p => p.FlowID == this.FlowGuid);
                    {
                        flow.StateID = (int)FlowState.Published;
                        dc.SaveChanges();
                    }
                }
                return this.FlowGuid;
            }
            catch (Exception ex)
            {
                Log.WriteError(string.Format("{0}({1}): Published is failed....", this.MobileType, this.MobileVersion));
                Log.WriteError(ex.Message);
                Log.WriteError(ex.StackTrace);
                return Guid.Empty;
            }
        }

        //public EntityObject ToDataBaseFormat()
        //{
        //    using (var dc = PMSEntities.Create())
        //    {
        //        DeleteRelease(dc);

        //        if (WebState != 0)
        //            ReleaseSate(WebState, dc);

        //        if (WapState != 0)
        //            ReleaseSate(WapState, dc);

        //        if (PreloadedState != 0)
        //            ReleaseSate(PreloadedState, dc);

        //        if (TysxSpaceState != 0)
        //            ReleaseSate(TysxSpaceState, dc);

        //        if (TysxBoLuState != 0)
        //            ReleaseSate(TysxBoLuState, dc);

        //        return new tysx_AppFlow()
        //        {
        //            StateID = (int)FlowState.Published,
        //        };
        //    }
        //}
    }
}